package com.jacobson.huobi.mvp.adapters

import android.content.Context
import android.graphics.Color
import android.graphics.Paint
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.jacobson.core.di.annotation.ActivityScope
import com.jacobson.core.widget.adapter.ListRecyclerAdapter
import com.jacobson.huobi.R
import com.jacobson.huobi.common.constants.UserTypeConstant
import com.jacobson.huobi.components.CommonListRecyclerAdapter
import com.jacobson.huobi.entities.*
import com.jacobson.huobi.entities.vo.RechargeRecordVo
import com.jacobson.huobi.mvp.contract.VipContract
import com.jacobson.huobi.utils.BigImgUtil
import com.jacobson.huobi.utils.ResourcesUtil
import kotlinx.android.synthetic.main.item_vip_recharge.view.*
import kotlinx.android.synthetic.main.item_vip_recharge_record.view.*
import java.text.DecimalFormat
import javax.inject.Inject
import javax.inject.Named


@ActivityScope
class ReChargeRecordAdapter @Inject constructor(@Named("context") context: Context) :
    CommonListRecyclerAdapter<RechargeRecordVo>(context) {
    @field:Transient
    private var expandPosition: Int = 0

    override fun onCreateContentViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ContentViewHolder<RechargeRecordVo> =
        ViewHolder(context, parent)

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as? ViewHolder)?.setExpendPosition(this.expandPosition)
        super.onBindViewHolder(holder, position)
    }

    fun setExpandPosition(position: Int) {
        if (this.expandPosition == position) {
            this.expandPosition = 0
        } else {
            this.expandPosition = position
        }
        notifyDataSetChanged()
    }

    class ViewHolder(
        context: Context?,
        parent: ViewGroup
    ) : ListRecyclerAdapter.ContentViewHolder<RechargeRecordVo>(context, parent, R.layout.item_vip_recharge_record) {

        private var expendPosition: Int = 0

        fun setExpendPosition(position: Int) {
            this.expendPosition = position
        }

        override fun onBindView(items: List<RechargeRecordVo>, item: RechargeRecordVo, position: Int) {
            with(itemView) {
                tvVipRecordLevelPre.text = UserTypeConstant.getUserTypeDesc(item.vip_level_pre)
                tvVipRecordLevelAfter.text = UserTypeConstant.getUserTypeDesc(item.vip_level_after)
                tvVipRecordRechargeType.text = UserTypeConstant.getRechargeTypeDesc(item.recharge_type)
                tvVipRecordNum.text = ("数量:" + item.num)
                tvVipRecordConsumption.text = ("交易额:" + item.consumption + "$")
                tvVipRecordTime.text = (item.begin_time_pre + " -- " + item.begin_time_after)
                tvVipRecordTranId.text = ("交易ID:" +  item.transaction_id)
//                tvVipRecordUrl.setImageURI(item.screenshotUrl)
                Glide.with(this).load(item.screenshot_url).into(tvVipRecordUrl)
                tvVipRecordUrl.setOnClickListener {
                    BigImgUtil.openBigImg(this.context, item.screenshot_url)
                }
//                tvDescription.text = item.description
//                tvDurationDesc.text = item.durationDesc
//                tvRealPrice.text = ("$" + ResourcesUtil.formatString(R.string.fm_symbol_double_one, item.realPrice))
//                tvNominalPrice.text = ("$" + ResourcesUtil.formatString(R.string.fm_symbol_double_one, item.nominalPrice))
//                tvNominalPrice.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG //设置中划线
//                if (position == expendPosition)  {
//                    this.setBackgroundResource(R.drawable.ic_vip_recharge_tab_item_selected)
//                }else {
//                    this.setBackgroundColor(Color.WHITE)
//                }
            }
        }


    }
}

private val decimalFormat = DecimalFormat("###################.###########")
private fun Double.ignoreEndZero() = decimalFormat.format(this)
